package com.oepm.web.controller;

import com.oepm.biz.ApplyVerifyBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.EstabStuInfoPo;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.dao.entity.StuDemoFormPo;
import com.oepm.dao.entity.vo.StuOneVo;
import com.oepm.vo.DataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@CrossOrigin//解决跨域
@RestController
@Api(tags = "教师审核")
@RequestMapping("/applyVerify")
public class ApplyVerifyController {

    @Autowired
    private ApplyVerifyBiz applyVerifyBiz;
    @GetMapping("/select")
    @ApiOperation("查询需要审核的项目项目")
    @RequiresRoles(value = {"4"})
    public Result selectTeaForm(ServletRequest request,QueryInfo queryInfo){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        DataVo dataVo = applyVerifyBiz.queryForms(userId, queryInfo);
        Result result = new Result(Meta.SUCCESS);
        result.setData(dataVo);
        return result;
    }

    @GetMapping("/selectId/{sname}")
    @ApiOperation("根据学生姓名查询已申报项目")
    @RequiresRoles(value = {"4"})
    public Result selectTeaForm(ServletRequest request,@PathVariable String sname){
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        String userId = JWTUtil.getUserId(token);
        HashMap<String,String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("sname", sname);
        List<StuDemoFormPo> stuDemoFormPos = applyVerifyBiz.queryFormByUsername(map);
        Result result = new Result(Meta.SUCCESS);
        result.setData(stuDemoFormPos);
        return result;
    }


    @PostMapping("/update")
    @ApiOperation("审核学生申报项目")
    @RequiresRoles(value = {"4"})
    public Result updateTeaForm(@RequestBody StuDemoFormPo stuDemoFormPo){
        int i = applyVerifyBiz.updateTeaView(stuDemoFormPo);
        int flag1,flag2;
        if(stuDemoFormPo.getTeacher_status().equals("已通过")){
            //获取项目pid
            String pid = stuDemoFormPo.getPid();
            //查询数据库学生名
            String name0 = applyVerifyBiz.selectStus(pid);
            String names;//中间值传递作用
            String sname = stuDemoFormPo.getSname();//当前学生名
            //获取专业名
            String major_grade0 = applyVerifyBiz.selectMajor(pid);
            String major_grades = null;//中间值
            String smajor_grade = stuDemoFormPo.getMajor_grade();//当前学生专业年级

            if (major_grade0==null || major_grade0.equals("")){
                major_grades = smajor_grade;
            }else{
                major_grades = major_grade0;
                String[] split0 = major_grade0.split("\\ ");//分割学生专业年级
                List<String> list = Arrays.asList(split0);
                if (!list.contains(smajor_grade)){
                    major_grades = major_grade0.concat(" "+smajor_grade);
                }
            }
            EstabStuInfoPo info0 = new EstabStuInfoPo();
            info0.setPid(pid);
            info0.setMajor_grade(major_grades);
            flag2 = applyVerifyBiz.updateEstabMajor(info0);
            if (flag2<=0){
                return new Result(Meta.FAIL);
            }
            if(name0==null || name0.equals("")){
                names = sname;
            }else{
                names = name0.concat(" "+sname);
            }
            //获取学生数
            String[] split = names.split("\\ ");//分割学生名
            int num = split.length;
            //更新已选人数
            EstabStuInfoPo info = new EstabStuInfoPo(pid,num);
            applyVerifyBiz.updateStuNum(info);
            //更新学生名
            EstabStuInfoPo info1 = new EstabStuInfoPo(pid,names);
            flag2 = applyVerifyBiz.updateStuName(info1);
            if (flag2<=0){
                return new Result(Meta.FAIL);
            }
        }
/*        else if (stuDemoFormPo.getTeacher_status().equals("已驳回")){

        }*/
        if(i>0){
            return new Result(Meta.SUCCESS);
        }
        return new Result(Meta.FAIL);
    }

    /**
     * 教师添加学生负责人
     * @param info
     * @return
     */
    @PostMapping("/updateStu")
    public Result updateStuOne(@RequestBody StuOneVo info){
        Result result;
        //查询
        List<StuOneVo> list = applyVerifyBiz.selectStuOnes(info.getPid());
        for (StuOneVo s:list){
            System.out.println(s.toString());
        }
        for (int i=0;i<list.size();i++){
            String flag = list.get(i).getIf_stuone();
            if (flag==null || flag.equals("")){
                continue;
            }else if(list.get(i).getIf_stuone().equals("是")){
                applyVerifyBiz.deleteStuOne(list.get(i).getId());
                break;
            }
        }
        info.setIf_stuone("是");
        int flag1 = applyVerifyBiz.updateStuOne(info);
        int flag2 = applyVerifyBiz.updataEstabStuOne(info);
        if (flag1>0 & flag2>0){
            result = new Result(Meta.SUCCESS);
        }else {
            result = new Result(Meta.FAIL);
        }
        return result;
    }
}